package com.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.entity.Medicine;
import com.hospital.entity.PrescriptionDetail;
import com.hospital.mapper.PrescriptionDetailMapper;
import com.hospital.service.MedicineService;
import com.hospital.service.PrescriptionDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 处方明细服务实现类
 */
@Service
@RequiredArgsConstructor
public class PrescriptionDetailServiceImpl extends ServiceImpl<PrescriptionDetailMapper, PrescriptionDetail> 
    implements PrescriptionDetailService {

    private final MedicineService medicineService;

    @Override
    public List<PrescriptionDetail> getByPrescriptionId(Long prescriptionId) {
        return baseMapper.selectByPrescriptionId(prescriptionId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<PrescriptionDetail> details) {
        if (details == null || details.isEmpty()) {
            return true;
        }

        // 填充药品信息和其他必要字段
        for (PrescriptionDetail detail : details) {
            Medicine medicine = medicineService.getById(detail.getMedicineId());
            if (medicine != null) {
                detail.setMedicineName(medicine.getName());
                detail.setMedicineSpecification(medicine.getSpecification());
                detail.setMedicineUnit(medicine.getUnit());
                if (detail.getUnitPrice() == null) {
                    detail.setUnitPrice(medicine.getSellingPrice());
                }
                // 计算小计金额
                detail.setAmount(detail.getUnitPrice().multiply(new java.math.BigDecimal(detail.getQuantity())));
            }
            
            // 设置其他必要字段
            detail.setId(null); // 确保ID为null，让数据库自动生成
            detail.setCreateTime(LocalDateTime.now());
            detail.setUpdateTime(LocalDateTime.now());
            detail.setDeleted(0);
        }

        return super.saveBatch(details);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByPrescriptionId(Long prescriptionId) {
        return baseMapper.deleteByPrescriptionId(prescriptionId) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updatePrescriptionDetails(Long prescriptionId, List<PrescriptionDetail> details) {
        // 先删除原有的明细（使用逻辑删除）
        baseMapper.deleteByPrescriptionId(prescriptionId);
        
        // 设置处方ID并保存新的明细
        if (details != null && !details.isEmpty()) {
            details.forEach(detail -> {
                detail.setId(null); // 确保ID为null，让数据库自动生成
                detail.setPrescriptionId(prescriptionId);
                detail.setCreateTime(LocalDateTime.now());
                detail.setUpdateTime(LocalDateTime.now());
                detail.setDeleted(0);
            });
            return saveBatch(details);
        }
        
        return true;
    }
} 